package com.ms.dsq_english.wall

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.ms.dsq_english.R
import com.ms.dsq_english.adapter.RVAdapterAreaCurrentPart
import com.ms.dsq_english.http.HttpManager
import com.ms.dsq_english.model.PartSalesBean
import com.ms.dsq_english.utils.LogUtils
import com.ms.dsq_english.utils.MapUtilsParamNull
import com.ms.dsq_english.utils.ResultCheckUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.frag_part.*
import java.util.ArrayList
import java.util.HashMap

/**
 *
 * @Description:
 * @Author:         zheng
 */
class PartFFragment : Fragment()
{
    private lateinit var mContext: Context
    private val list: MutableList<PartSalesBean.DataBean.SalesRegionBean> = ArrayList()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
    {
        mContext = context!!

        return inflater.inflate(R.layout.frag_part, container, false)
    }

    override fun onResume()
    {
        super.onResume()
        Glide.with(mContext).load(R.drawable.english_back_img).into(all_img)
        sales
    }

    fun refresh()
    {
        sales
    }

    @get:SuppressLint("CheckResult")
    private val sales: Unit
        private get()
        {
            val params: HashMap<String, String> = MapUtilsParamNull()
                    .getBaseMap(mContext)

            HttpManager.getRetrofitInterface()
                    .getPartSales(params)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe({ result: PartSalesBean ->
                        if (result.code == 10000)
                        {
                            initRecyclerViewCurrent(result.data.sale_detail)
                            text1.text = result.data.total_data.prestige_goods
                            text2.text = result.data.total_data.immu_goods
                            text3.text = result.data.total_data.gold_goods
                            text4.text = result.data.total_data.shop_number
                            text5.text = result.data.total_data.gold_shop_number
                            text6.text = result.data.total_data.dL_goods
                            text7.text = result.data.total_data.prestige_shop_number
                            text77.text = result.data.total_data.dL_shop_number

                            //东区
//                            textd1.text = result.data.zone_east.gold_goods
//                            textd2.text = result.data.zone_east.prestige_goods
//                            textd3.text = result.data.zone_east.shop_number
//                            textd4.text = result.data.zone_east.prestige_shop_number
//                            textd5.text = result.data.zone_east.gold_shop_number
//                            textd6.text = result.data.zone_east.dL_goods
//                            textd7.text = result.data.zone_east.dL_shop_number
//                            textd55.text = result.data.zone_east.dL_shop_number

                            textw1.text = result.data.zone_east.prestige_goods
                            textw2.text = result.data.zone_east.dL_goods
                            textw3.text = result.data.zone_east.gold_goods
                            textw4.text = result.data.zone_east.immu_goods
                            textw5.text = result.data.zone_east.shop_number
                            textw6.text = result.data.zone_east.prestige_shop_number
                            textw7.text = result.data.zone_east.dL_shop_number
                            textw8.text = result.data.zone_east.gold_shop_number


                            //西区
                            textx1.text = result.data.zone_west.prestige_goods
                            textx2.text = result.data.zone_west.dL_goods
                            textx3.text = result.data.zone_west.gold_goods
                            textx4.text = result.data.zone_west.immu_goods
                            textx5.text = result.data.zone_west.shop_number
                            textx6.text = result.data.zone_west.prestige_shop_number
                            textx7.text = result.data.zone_west.dL_shop_number
                            textx8.text = result.data.zone_west.gold_shop_number

                            //南区
                            textn1.text = result.data.zone_south.prestige_goods
                            textn2.text = result.data.zone_south.dL_goods
                            textn3.text = result.data.zone_south.gold_goods
                            textn4.text = result.data.zone_south.immu_goods
                            textn5.text = result.data.zone_south.shop_number
                            textn6.text = result.data.zone_south.prestige_shop_number
                            textn7.text = result.data.zone_south.dL_shop_number
                            textn8.text = result.data.zone_south.gold_shop_number

                            //北区
                            textb1.text = result.data.zone_north.prestige_goods
                            textb2.text = result.data.zone_north.dL_goods
                            textb3.text = result.data.zone_north.gold_goods
                            textb4.text = result.data.zone_north.immu_goods
                            textb5.text = result.data.zone_north.shop_number
                            textb6.text = result.data.zone_north.prestige_shop_number
                            textb7.text = result.data.zone_north.dL_shop_number
                            textb8.text = result.data.zone_north.gold_shop_number



                            if (result.data.sale_province.size == 0)
                            {
                                list1.text = ""
                                list2.text = ""
                                list3.text = ""
                                list4.text = ""
                                list5.text = ""
                            }
                            if (result.data.sale_province.size == 1)
                            {
                                list1.text = result.data.sale_province.get(0).region_province
                                list2.text = ""
                                list3.text = ""
                                list4.text = ""
                                list5.text = ""
                            }
                            if (result.data.sale_province.size == 2)
                            {
                                list1.text = result.data.sale_province.get(0).region_province
                                list2.text = result.data.sale_province.get(1).region_province
                                list3.text = ""
                                list4.text = ""
                                list5.text = ""
                            }
                            if (result.data.sale_province.size == 3)
                            {
                                list1.text = result.data.sale_province.get(0).region_province
                                list2.text = result.data.sale_province.get(1).region_province
                                list3.text = result.data.sale_province.get(2).region_province
                                list4.text = ""
                                list5.text = ""
                            }
                            if (result.data.sale_province.size == 4)
                            {
                                list1.text = result.data.sale_province.get(0).region_province
                                list2.text = result.data.sale_province.get(1).region_province
                                list3.text = result.data.sale_province.get(2).region_province
                                list4.text = result.data.sale_province.get(3).region_province
                                list5.text = ""
                            }
                            if (result.data.sale_province.size == 5)
                            {
                                list1.text = result.data.sale_province.get(0).region_province
                                list2.text = result.data.sale_province.get(1).region_province
                                list3.text = result.data.sale_province.get(2).region_province
                                list4.text = result.data.sale_province.get(3).region_province
                                list5.text = result.data.sale_province.get(4).region_province
                            }

                            if (result.data.sale_city.size == 0)
                            {
                                listc1.text = ""
                                listc2.text = ""
                                listc3.text = ""
                                listc4.text = ""
                                listc5.text = ""
                            }
                            if (result.data.sale_city.size == 1)
                            {
                                listc1.text = result.data.sale_city.get(0).region_province
                                listc2.text = ""
                                listc3.text = ""
                                listc4.text = ""
                                listc5.text = ""
                            }
                            if (result.data.sale_city.size == 2)
                            {
                                listc1.text = result.data.sale_city.get(0).region_province
                                listc2.text = result.data.sale_city.get(1).region_province
                                listc3.text = ""
                                listc4.text = ""
                                listc5.text = ""
                            }
                            if (result.data.sale_city.size == 3)
                            {
                                listc1.text = result.data.sale_city.get(0).region_province
                                listc2.text = result.data.sale_city.get(1).region_province
                                listc3.text = result.data.sale_city.get(2).region_province
                                listc4.text = ""
                                listc5.text = ""
                            }
                            if (result.data.sale_city.size == 4)
                            {
                                listc1.text = result.data.sale_city.get(0).region_province
                                listc2.text = result.data.sale_city.get(1).region_province
                                listc3.text = result.data.sale_city.get(2).region_province
                                listc4.text = result.data.sale_city.get(3).region_province
                                listc5.text = ""
                            }
                            if (result.data.sale_city.size == 5)
                            {
                                listc1.text = result.data.sale_city.get(0).region_province
                                listc2.text = result.data.sale_city.get(1).region_province
                                listc3.text = result.data.sale_city.get(2).region_province
                                listc4.text = result.data.sale_city.get(3).region_province
                                listc5.text = result.data.sale_city.get(4).region_province
                            }

                            //地图
                            list.clear()
                            list.addAll(result.data.sales_region)
                            map1.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "新疆维吾尔自治区")
                                    {
                                        textnum.text = "新疆维吾尔自治区门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map2.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "西藏自治区")
                                    {
                                        textnum.text = "西藏自治区门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map3.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "内蒙古自治区")
                                    {
                                        textnum.text = "内蒙古自治区门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map4.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "青海省")
                                    {
                                        textnum.text = "青海省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map5.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "甘肃省")
                                    {
                                        textnum.text = "甘肃省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map6.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "黑龙江省")
                                    {
                                        textnum.text = "黑龙江省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map7.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "吉林省")
                                    {
                                        textnum.text = "吉林省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map8.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "辽宁省")
                                    {
                                        textnum.text = "辽宁省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map9.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "河北省")
                                    {
                                        textnum.text = "河北省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map10.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "北京")
                                    {
                                        textnum.text = "北京门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map11.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "天津")
                                    {
                                        textnum.text = "天津门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map12.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "山东省")
                                    {
                                        textnum.text = "山东省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map13.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "江苏省")
                                    {
                                        textnum.text = "江苏省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map14.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "浙江省")
                                    {
                                        textnum.text = "浙江省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map15.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "福建省")
                                    {
                                        textnum.text = "福建省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map16.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "广东省")
                                    {
                                        textnum.text = "广东省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map17.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "广西壮族自治区")
                                    {
                                        textnum.text = "广西壮族自治区门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map18.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "云南省")
                                    {
                                        textnum.text = "云南省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map19.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "四川省")
                                    {
                                        textnum.text = "四川省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map20.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "甘肃省")
                                    {
                                        textnum.text = "甘肃省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map21.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "贵州省")
                                    {
                                        textnum.text = "贵州省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map22.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "湖南省")
                                    {
                                        textnum.text = "湖南省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map23.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "江西省")
                                    {
                                        textnum.text = "江西省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map24.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "重庆")
                                    {
                                        textnum.text = "重庆门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map25.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "湖北省")
                                    {
                                        textnum.text = "湖北省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map26.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "安徽省")
                                    {
                                        textnum.text = "安徽省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map27.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "河南省")
                                    {
                                        textnum.text = "河南省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map28.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "陕西省")
                                    {
                                        textnum.text = "陕西省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map29.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "山西省")
                                    {
                                        textnum.text = "山西省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map30.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "宁夏回族自治区")
                                    {
                                        textnum.text = "宁夏回族自治区门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                            map31.setOnClickListener {
                                var i = 0
                                while (i < list.size)
                                {
                                    if (list[i].pname == "海南省")
                                    {
                                        textnum.text = "海南省门店数：\n" + list[i].nums
                                    }
                                    i++
                                }
                            }
                        }
                        else
                        {
                            ResultCheckUtils().checkResult(mContext, result.code, result.msg)
                        }
                    }
                    ) { e: Throwable -> LogUtils.showLog(HttpManager.TAG, e.message) }
        }

    private fun initRecyclerViewCurrent(list: List<PartSalesBean.DataBean.SaleDetailBean>)
    {
        part_rec.layoutManager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        part_rec.adapter = RVAdapterAreaCurrentPart(mContext, list)
        part_rec.start()
    }
}